using Domain;
using ErrorCode;
using Infrastructure.Data;
using MediatR;
using SqlSugar;
using SqlSugar.Extensions;
using WriteAPI.Command.Material;
using static System.Net.Mime.MediaTypeNames;

namespace WriteAPI.CommandHandle.Material
{

    public class GetMaterialDetailCommandHandler : IRequestHandler<GetMaterialDetailCommand, MaterialReplenishment>
    {
        private readonly ISqlSugarClient db;


        public GetMaterialDetailCommandHandler(ISqlSugarClient db)
        {
            this.db = db;

        }

        public async Task<MaterialReplenishment> Handle(GetMaterialDetailCommand request, CancellationToken cancellationToken)
        {

            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine("Generated SQL:\n" + sql);
            };

            // 查询主表并包含子表数据
            var result = await db.Queryable<MaterialReplenishment>()
                .Includes(x => x.MaterialDetails)
                .Where(x => x.DocCode == request.DocCode)
                .FirstAsync();


            return result;


        }
    }
}
